<template>
  <div class="home-container">
    <header class="header">
      <nav class="nav-container">
        <!-- Logo -->
        <el-row class="w-full" align="middle" justify="space-between">
          <el-col :span="3">
            <div class="logo-wrapper animate__animated animate__fadeIn">
              <img src="@/imgs/logo.png" alt="Logo" class="logo" @click="navigateToHome">
            </div>
          </el-col>

          <!-- Navigation Menu -->
          <el-col :span="8">
            <ul class="nav-menu animate__animated animate__fadeIn">
              <li class="nav-item">
                <t-icon name="star" />
                <span>推荐</span>
              </li>
              <li class="nav-item" @click="router.push('/home/articleList')">
                <t-icon name="book" />
                <span>文章</span>
              </li>
              <li class="nav-item" @click="router.push('/home/questionList')">
                <t-icon name="help-circle" />
                <span>问题</span>
              </li>
              <li class="nav-item">
                <t-icon name="view-module" />
                <span>专栏</span>
              </li>
            </ul>
          </el-col>

          <!-- Search Bar -->
          <el-col :span="8">
            <div class="search-wrapper animate__animated animate__fadeIn">
              <div class="search-box">
                <t-icon name="search" class="search-icon"/>
                <input 
                  type="search"
                  placeholder="Search..."
                  class="search-input"
                  @keydown.enter="handleSearch"
                  v-model="searchQuery"
                >
                <div class="search-button-wrapper">
                  <button class="search-button" @click="handleSearch">
                    <t-icon name="search" />
                  </button>
                </div>
              </div>
            </div>
          </el-col>

          <!-- User Actions -->
          <el-col :span="5">
            <div class="user-actions animate__animated animate__fadeIn">
              <!-- 消息通知图标 -->
              <div class="notification-dropdown">
                <t-icon 
                  name="mail" 
                  class="notification-icon" 
                  @mouseenter="handleNotificationHover"
                  @mouseleave="showNotifications = false"
                  :class="{ 'notification-icon-expanded': showNotifications }"
                  @click="navigateToMessage"
                />
                <transition name="notification-fade">
                  <div 
                    v-show="showNotifications"
                    class="notification-dropdown-content"
                    @mouseenter="showNotifications = true"
                    @mouseleave="showNotifications = false"
                  >
                    <div class="notification-menu">
                      <div class="notification-item" @click="navigateToMessageTab('like')">
                        <t-icon name="thumb-up" />
                        <span>收到的赞</span>
                        <span v-if="profile.unreadLikeCount" class="notification-count">{{ profile.unreadLikeCount }}</span>
                      </div>
                      <div class="notification-item" @click="navigateToMessageTab('reply')">
                        <t-icon name="chat-message" />
                        <span>回复我的</span>
                        <span v-if="profile.unreadCommentCount" class="notification-count">{{ profile.unreadCommentCount }}</span>
                      </div>
                      <div class="notification-item" @click="navigateToMessageTab('chat')">
                        <t-icon name="chat-double" />
                        <span>我的私信</span>
                      </div>
                      <div class="notification-item" @click="navigateToMessageTab('system')">
                        <t-icon name="notification" />
                        <span>系统通知</span>
                      </div>
                    </div>
                  </div>
                </transition>
              </div>
              
              <div class="dropdown">
                <div 
                  class="avatar"
                  :style="{ backgroundImage: `url(${getAvatarUrl()})` }"
                  :class="{ 'avatar-expanded': showProfile }"
                  @mouseenter="handleAvatarHover"
                  @mouseleave="showProfile = false"
                ></div>
                <div 
                  class="dropdown-content"
                  :class="{ 'show': showProfile }"
                  @mouseenter="showProfile = true"
                  @mouseleave="showProfile = false"
                >
                  <div class="profile-header">
                    <div class="profile-info">
                      <h3>{{ profile.username }}</h3>
                      <p>{{ profile.description || '这个人很懒，什么都没有留下...' }}</p>
                    </div>
                  </div>

                  <div class="profile-stats">
                    <div class="stat-item">
                      <span class="number">{{ profile.likeCount }}</span>
                      <span class="label">Likes</span>
                    </div>
                    <div class="stat-item" @click="navigateToFollowList('followers')">
                      <span class="number">{{ profile.followerCount }}</span>
                      <span class="label">Followers</span>
                    </div>
                    <div class="stat-item" @click="navigateToFollowList('following')">
                      <span class="number">{{ profile.followingCount }}</span>
                      <span class="label">Following</span>
                    </div>
                  </div>

                  <div class="profile-actions">
                    <button class="action-btn" @click="navigateToProfile">
                      <t-icon name="user" />
                      个人资料
                    </button>
                    <button class="action-btn" @click="navigateToManuscriptManagement">
                      <t-icon name="setting" />
                      稿件管理
                    </button>
                    <button class="action-btn logout" @click="handleLogout">
                      <t-icon name="logout" />
                      退出登陆
                    </button>
                  </div>
                </div>
              </div>

              <button class="upload-btn" @click="router.push('/home/upload')">
                <t-icon name="upload" />
                Upload
              </button>
            </div>
          </el-col>
        </el-row>
      </nav>
    </header>

    <main class="main-content">
      <router-view v-slot="{ Component }">
        <transition name="fade" mode="out-in">
          <component :is="Component" />
        </transition>
      </router-view>
    </main>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router';
import { useCookies } from '@vueuse/integrations/useCookies';
import { ref, onMounted, computed } from 'vue';

import { storeToRefs } from 'pinia';
import { ElMessage } from 'element-plus';
import 'animate.css';
import defaultAvatar from '@/imgs/avatar.png';
import { useUserStore } from '@/stores/userStore';
// 获取 userStore 实例
const userStore = useUserStore();
const { profile } = storeToRefs(userStore);

const cookie = useCookies();
const router = useRouter();
const showProfile = ref(false);
const showNotifications = ref(false);
const searchQuery = ref('');

const getAvatarUrl = () => {
  if (!profile.value.profilePicture || profile.value.profilePicture === '') {
    return defaultAvatar;
  }
  return profile.value.profilePicture;
};



const handleLogout = () => {
  cookie.remove("user-token", { path: '/' });
  ElMessage({
    message: '已成功退出登录',
    type: 'success',
    duration: 2000
  });
  router.push("/");
};

const handleSearch = () => {
  if (!searchQuery.value.trim()) {
    ElMessage({
      message: '请输入搜索内容',
      type: 'warning',
      duration: 2000
    });
    return;
  }
  
  router.push(`/home/search/${searchQuery.value}`);
};

const navigateToProfile = () => {
  router.push(`/home/userCenter/${profile.value.id}`);
};

const navigateToManuscriptManagement = () => {
  router.push("/home/contentManagement");
};

const navigateToHome = () => {
  router.push("/home");
};

const navigateToMessage = () => {
  router.push(`/home/message/${profile.value.id}`);
};

const navigateToMessageTab = (tab) => {
  router.push({
    path: `/home/message/${profile.value.id}/${tab}`,
  });
};

onMounted(() => {
  // 可以在这里添加页面加载时的动画效果
  console.log('Home component mounted');
});
const handleAvatarHover = async () => {
  try {
    await userStore.getProfile();
    showProfile.value = true;
  } catch (error) {
    console.error('获取用户信息失败:', error);
    ElMessage.error('获取用户信息失败，请稍后重试');
  }
};

const handleNotificationHover = async () => {
  try {
    await userStore.getProfile();
    showNotifications.value = true;
  } catch (error) {
    console.error('获取用户信息失败:', error);
    ElMessage.error('获取用户信息失败，请稍后重试');
  }
};

const navigateToFollowList = (type) => {
  // 这里的 type 可以是 'followers' 或 'following'
  // 假设 followList 组件的路由路径可以通过传递参数来指定显示的列表类型
  router.push({
    name: 'followList', 
    params: { 
      id: profile.value.id, 
      tab: type 
    }
  });
};
</script>

<style scoped>
/* 全局样式 */
body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  margin: 0;
  padding: 0;
}

.home-container {
  min-height: 100vh;
  background-color: #f9f9f9;
}

/* 上边栏样式 */
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: #ffffff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  z-index: 100;
  backdrop-filter: blur(10px);
}

/* 导航容器样式 */
.nav-container {
  max-width: 1600px;
  height: 100%;
  margin: 0 auto;
  padding: 0 32px;
}

/* Logo样式 */
.logo-wrapper {
  flex-shrink: 0;
  padding: 10px 0;
  transition: transform 0.3s ease;
}

.logo-wrapper:hover {
  transform: scale(1.05);
}

.logo {
  height: 40px;
  width: auto;
  object-fit: contain;
  cursor: pointer;
  transition: filter 0.3s ease;
}

.logo:hover {
  filter: brightness(1.1);
}

/* 导航菜单样式 */
.nav-menu {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  gap: 24px;
  height: 100%;
  align-items: center;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 8px;
  transition: all 0.3s ease;
  white-space: nowrap;
  color: #606266;
  font-weight: 500;
  position: relative;
}

.nav-item:hover {
  color: #409EFF;
  background-color: rgba(64, 158, 255, 0.1);
  transform: translateY(-2px);
}

.nav-item::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 50%;
  width: 0;
  height: 2px;
  background-color: #409EFF;
  transition: all 0.3s ease;
  transform: translateX(-50%);
}

.nav-item:hover::after {
  width: 70%;
}

/* 搜索栏样式 */
.search-wrapper {
  width: 100%;
  padding: 10px 0;
}

.search-box {
  position: relative;
  width: 100%;
  max-width: 450px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  transition: all 0.3s ease;
}

.search-box:focus-within {
  transform: scale(1.02);
}

.search-icon {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #909399;
  transition: color 0.3s ease;
}

.search-input {
  width: 100%;
  height: 40px;
  padding: 5px 50px 5px 45px;
  border: none;
  border-radius: 20px;
  outline: none;
  transition: all 0.3s ease;
  font-size: 14px;
  color: #303133;
  background-color: #f5f7fa;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.search-input:focus {
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.3);
  background-color: #ffffff;
}

.search-input:focus + .search-icon {
  color: #409EFF;
}

.search-button-wrapper {
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
  width: 30px;
  height: 30px;
  background-color: #409EFF;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.3s ease;
}

.search-button-wrapper:hover {
  background-color: #337ecc;
  transform: translateY(-50%) scale(1.05);
}

.search-button {
  background: none;
  border: none;
  cursor: pointer;
  color: white;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 用户操作样式 */
.user-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 20px;
  padding: 10px 0;
}

/* 通知图标样式 */
.notification-dropdown {
  position: relative;
}

.notification-icon {
  font-size: 22px;
  color: #606266;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  position: relative;
  z-index: 12;
}

.notification-icon:hover {
  color: #409EFF;
  transform: scale(1.1) translateY(-3px);
}

.notification-icon-expanded {
  transform: scale(1.8) translateY(15px);
  color: #409EFF;
}

.notification-dropdown-content {
  position: absolute;
  right: -20px;
  top: calc(100% + 10px);
  width: 200px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
  padding: 15px;
  z-index: 11;
  padding-top: 40px;
}

/* 通知下拉菜单过渡动画 */
.notification-fade-enter-active,
.notification-fade-leave-active {
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.notification-fade-enter-from,
.notification-fade-leave-to {
  opacity: 0;
  transform: translateY(-10px);
}

.notification-menu {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.notification-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 15px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 8px;
  color: #606266;
  position: relative;
}

.notification-item:hover {
  color: #409EFF;
  background-color: rgba(64, 158, 255, 0.1);
  transform: translateX(5px);
}

.notification-count {
  position: absolute;
  right: 15px;
  background-color: #f56c6c;
  color: white;
  border-radius: 10px;
  padding: 2px 8px;
  font-size: 12px;
  font-weight: bold;
}

.dropdown {
  position: relative;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
  border: 2px solid transparent;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  z-index: 12;
  position: relative;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.avatar-expanded {
  transform: scale(1.8) translateY(15px);
  border-color: #409EFF;
  box-shadow: 0 8px 25px rgba(64, 158, 255, 0.3);
}

.dropdown-content {
  display: none;
  position: absolute;
  right: -20px;
  top: calc(100% + 10px);
  width: 300px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
  padding: 25px;
  opacity: 0;
  transform: translateY(-10px);
  transition: all 0.3s ease;
  z-index: 11;
  padding-top: 40px;
}

.dropdown-content.show {
  display: block;
  opacity: 1;
  transform: translateY(0);
}

.profile-header {
  display: flex;
  align-items: center;
  gap: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid #ebeef5;
  position: relative;
}

.profile-info h3 {
  margin: 0;
  font-size: 18px;
  color: #303133;
  font-weight: 600;
}

.profile-info p {
  margin: 6px 0 0;
  color: #909399;
  font-size: 14px;
}

.profile-stats {
  display: flex;
  justify-content: space-around;
  padding: 20px 0;
  border-bottom: 1px solid #ebeef5;
}

.stat-item {
  text-align: center;
  transition: transform 0.3s ease;
  cursor: pointer
}

.stat-item:hover {
  transform: translateY(-3px);
}

.number {
  display: block;
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 5px;
}

.label {
  color: #909399;
  font-size: 14px;
}

.profile-actions {
  padding-top: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.action-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  border: none;
  background: none;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 8px;
  font-size: 15px;
  color: #606266;
  text-align: left;
}

.action-btn:hover {
  color: #409EFF;
  background-color: rgba(64, 158, 255, 0.1);
  transform: translateX(5px);
}

.logout {
  color: #f56c6c;
}

.logout:hover {
  color: #f56c6c;
  background-color: rgba(245, 108, 108, 0.1);
}

.upload-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: #409EFF;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.upload-btn:hover {
  background: #66b1ff;
  transform: translateY(-3px);
  box-shadow: 0 6px 16px rgba(64, 158, 255, 0.4);
}

.upload-btn:active {
  transform: translateY(-1px);
}

/* 主内容区域样式 */
.main-content {
  margin-top: 60px;
  padding: 24px;
  min-height: calc(100vh - 60px);
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>